package com.mku.wms_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mku.wms_service.common.QueryPageParam;
import com.mku.wms_service.common.Response;
import com.mku.wms_service.dto_vo.RecordVO;
import com.mku.wms_service.entity.Goods;
import com.mku.wms_service.entity.Record;
import com.mku.wms_service.mapper.RecordMapper;
import com.mku.wms_service.mapper.RecordVOMapper;
import com.mku.wms_service.service.IGoodsService;
import com.mku.wms_service.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maoxx
 * @since 2024-11-22
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private RecordVOMapper recordVOMapper;

    @Override
    public Response<Boolean> add(Record record) {

        Goods goods = goodsService.getById(record.getGoods());

        record.setCreatetime(LocalDateTime.now());
        // 出库
        int count;
        if ("2".equals(record.getAction())) {
            count = goods.getCount() - record.getCount();
        } else {
            count = goods.getCount() + record.getCount();
        }
        goods.setCount(count);
        goodsService.updateById(goods);
        return save(record) ? Response.success() : Response.fail("添加数据失败！");
    }

    @Override
    public Response<IPage<RecordVO>> listPage(QueryPageParam qp2) {
        Page<RecordVO> page = new Page<>();
        //QueryWrapper<RecordVO> qw = new QueryWrapper<>();

        page.setSize(qp2.getPageSize()).setCurrent(qp2.getPageNum());

        String name = (String) qp2.getParam().get("name");
        Integer storage = (Integer) qp2.getParam().get("storageId");
        Integer goodstype = (Integer) qp2.getParam().get("goodsTypeId");

        //System.out.println("storage: "+storage+"  goodstype: "+goodstype);

        /*if (name != null && !"".equals(name.trim())) {
            qw.like("name", name);
        }

        if (storage != null && storage >= 0) {
            qw.eq("storage", storage);
        }

        if (goodstype != null && goodstype > 0) {
            qw.eq("goodstype", goodstype);
        }*/

        IPage<RecordVO> pages = recordVOMapper.listPage(page, name, storage, goodstype);
        return Response.success(pages);
    }
}
